//
//  LYTestListViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/19.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYTestListViewController.h"
#import "LYVideoModel.h"
#import "LYTestListCell.h"
#import "LYTestViewController.h"
@interface LYTestListViewController ()
@property (weak, nonatomic) IBOutlet VVYRefreshTableView *tableView;

@property (nonatomic, strong) NSMutableArray *models;

@property (nonatomic, assign) NSInteger page;

@end

@implementation LYTestListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView.mj_header beginRefreshing];
}




#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    
    [LYRequestTool getOnlineTestListWithPage:@"1" per_page:@"10" success:^(NSArray<LYTestListModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];

        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];

    
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    
    
    [LYRequestTool getOnlineTestListWithPage:page per_page:@"10" success:^(NSArray<LYTestListModel *> *models) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}

#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYTestListModel *model = self.models[indexPath.row];
    LYTestViewController *testVC = [LYTestViewController testVCWithTestID:model.Id];
    [self.navigationController pushViewController:testVC animated:YES];
}

#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYTestCell";
    
    LYTestListCell *cell = [LYTestListCell cellWithTableView:tableView andIndetifier:cellID];
    
    LYTestListModel *model = self.models[indexPath.row];
    
    cell.model = model;
    
    return cell;
    
}



@end
